package LeetCode.month01.day30;

import java.util.HashMap;

public class No2958 {
    public static void main(String[] args) {
        System.out.println(maxSubarrayLength(new int[]{5,5,5,5,5,5,5},4));
    }
    public static int maxSubarrayLength(int[] nums, int k) {
        int n = nums.length;
        int left = 0;
        int res = 0;
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int index = nums[i];
            map.put(index,map.getOrDefault(index,0)+1);
            while(map.get(index)>k){
                map.put(nums[left],map.get(nums[left])-1);
                left++;
            }
            res = Math.max(res,i-left+1);
        }
        return res;
    }
}
